# model

# responsible for the access-control-list management
# the current user is determined with his files in the bucket for a specific storage (credentials)
# the user can chose from a set of rights what the acl for the file should be

from google.appengine.ext import webapp

from controller.BuildAclInfo import *

from Utilities import *

class ChangeACL(webapp.RequestHandler):
    def post(self):
        utilities = Utilities()
        
        # get some parameter from the url
        key = self.request.get('key')
        md5 = self.request.get('md5')
        acl = self.request.get('acl')
                
        # get some basic parameter
        username = utilities.getUsername()
        userText = utilities.getUserText()
        
        if username:
            # get some basic place-holders - not necessary for this class, but for the print in the responsible view (html)
            urlText = utilities.getUrlTextLogout()
            url = utilities.getLogout(self)
            bucketname = utilities.getBucketname(username)
            
            # get the available credentials
            credentials = utilities.getCredentials(username)
            results = credentials.fetch(100)
            
            if credentials:
                for result in results:
                    regionname = result.regionname
                    
                    if regionname == utilities.getAmazonText():
                        # get the storage-connection and bucket-instance                                                
                        amazonStorageConnection = utilities.getStorageServiceConnectionByResult(username, result)
                        amazonBucketInstance = utilities.getBucketInstance(amazonStorageConnection, bucketname)
                        
                        # prepare to get the key-permissions
                        amazonKeyInstance = amazonBucketInstance.get_acl(key)
                        amazonAcl = amazonBucketInstance.get_acl(key_name=key)
                        amazonPermissions = utilities.checkACL(utilities, amazonKeyInstance, amazonAcl)
                        
                        # get the key-list
                        amazonKeyList = utilities.getBucketKeyList(amazonStorageConnection, bucketname)
                        
                        # find the current key (by md5-checksum) in the key-list
                        amazonMD5Match = self.getMd5FromKeyList(username, utilities, md5, regionname, amazonKeyList)
                        
                        # check result...
                        if amazonMD5Match != None:
                            # set the new acl-settings
                            try:
                                amazonBucketInstance.set_acl(acl, key_name=key)
                            except:
                                # print hint-text
                                hintText = self.getErrorOccurredText()
                                hintColor = 'red'
                                
                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, amazonPermissions)
                            else:
                                # when it worked - print a success hint-text
                                hintText = self.getSuccessChangeText()
                                hintColor = 'green'

                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor,  amazonPermissions)
                                self.redirect('/viewacl?key=' + key + '&amp;md5hash=' + md5)
                        else:
                            # print hint-text
                            hintText = self.getErrorOccurredText()
                            hintColor = 'red'
                            
                            self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, amazonPermissions)
                        
                    if regionname == utilities.getGoogleText():
                        # get the storage-connection and bucket-instance
                        googleStorageConnection = utilities.getStorageServiceConnectionByResult(username, result)
                        googleBucketInstance = utilities.getBucketInstance(googleStorageConnection, bucketname)
                        
                        # prepare to get the key-permissions
                        googleKeyInstance = googleBucketInstance.get_acl(key)
                        googleAcl = googleBucketInstance.get_acl(key_name=key)
                        googlePermissions = utilities.checkACL(utilities, googleKeyInstance, googleAcl)
                        
                        # get the key-list
                        googleKeyList = utilities.getBucketKeyList(googleStorageConnection, bucketname)
                        
                        # find the current key (by md5-checksum) in the key-list
                        googleMD5Match = self.getMd5FromKeyList(username, utilities, md5, regionname, googleKeyList)
                        
                        # check result...
                        if googleMD5Match != None:
                            # set the new acl-settings
                            try:
                                googleBucketInstance.set_acl(acl, key_name=key)
                                
                            except:
                                # print hint-text
                                hintText = self.getErrorOccurredText()
                                hintColor = 'red'
                                
                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, googlePermissions)
                            else:
                                # when it worked - print a success hint-text
                                hintText = self.getSuccessChangeText()
                                hintColor = 'green'

                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor,  googlePermissions)
                                self.redirect('/viewacl?key=' + key + '&amp;md5hash=' + md5)
                        else:
                            # print hint-text
                            hintText = self.getErrorOccurredText()
                            hintColor = 'red'
                            
                            self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, googlePermissions)
                    
                    if regionname == utilities.getOpenStackText():
                        # get the storage-connection and bucket-instance
                        openStackStorageConnection = utilities.getStorageServiceConnectionByResult(username, result)
                        openStackBucketInstance = utilities.getBucketInstance(openStackStorageConnection, bucketname)
                        
                        # prepare to get the key-permissions
                        openStackKeyInstance = openStackBucketInstance.get_acl(key)
                        openStackAcl = openStackBucketInstance.get_acl(key_name=key)
                        openStackPermissions = utilities.checkACL(utilities, openStackKeyInstance, openStackAcl)
                        
                        # get the key-list
                        openStackKeyList = utilities.getBucketKeyList(openStackStorageConnection, bucketname)
                        
                        # find the current key (by md5-checksum) in the key-list
                        openStackMD5Match = self.getMd5FromKeyList(username, utilities, md5, regionname, openStackKeyList)
                        
                        # check result...
                        if openStackMD5Match != None:
                            # set the new acl-settings
                            try:
                                openStackBucketInstance.set_acl(acl, key_name=key)
                                
                            except:
                                # print hint-message
                                hintText = self.getErrorOccurredText()
                                hintColor = 'red'
                                
                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, openStackPermissions)
                            else:
                                # when it worked - print a success hint-message
                                hintText = self.getSuccessChangeText()
                                hintColor = 'green'
                                
                                self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, openStackPermissions)
                                self.redirect('/viewacl?key=' + key + '&amp;md5hash=' + md5)
                        else:
                            # print hint-message
                            hintText = self.getErrorOccurredText()
                            hintColor = 'red'
                            
                            self.callBuilder(key, md5, acl, userText, username, url, urlText, hintText, hintColor, openStackPermissions)
            else:
                self.redirect('/regions')
        else:
            self.redirect('/')
            
    
    # contains the error-message-text 
    def getErrorOccurredText(self):
#        hintText = _('An error occurred - the acl-settings could not be changed')
        hintText = 'An error occurred - the acl-settings could not be changed'
        return hintText
    
    # contians the success-message-text
    def getSuccessChangeText(self):
#        hintText = _('The change of the ACL was successful')
        hintText = 'The change of the ACL was successful'
        return hintText

    # responsible for the comparison of the given md5-hash-value with the key-list
    def getMd5FromKeyList(self, username, utilities, md5, regionname, keyList):
        md5Checksums = utilities.getMD5Checksums(username, regionname, keyList)
        
        if md5Checksums:
            for entry in md5Checksums:
                isMatch = cmp(entry, md5)

                if isMatch == 0:
                    return 'true'
                else:
                    return 'false'
        else:
            return None
    
    # responsible for the call of the get-method of the BuildAclInfo-Class
    def callBuilder(self, key, md5, acl, userText, username, url, urlText, hintText, hintColor, permissions):
        templateValues = {
                          'userText': userText,
                          'user': username,
                          'urlText': urlText,
                          'url': url,
                          'urlText': urlText,
                          'hintText': hintText,
                          'hintColor': hintColor
                          }
        builder = BuildAclInfo()
        builder.get(self, key, md5, acl, templateValues, permissions)


